<template>
  <div class="publish-container">
    <uni-card>
      <!-- 标题输入保持单行 -->
     
      
      <!-- 内容输入改为多行文本域 -->
      <view class="content-editor">
        <textarea 
          v-model="form.content" 
          placeholder="请输入内容（支持换行）"
          auto-height
          :show-confirm-bar="false"
          :disable-default-padding="true"
          class="text-area"
          @input="onContentInput"
        ></textarea>
      </view>

      <uni-section title="发布设置" type="line">
        <uni-forms-item label="匿名发布">
          <switch @change="onAnonymousChange" />
        </uni-forms-item>
      </uni-section>

      <button @click="handlePublish" type="primary">立即发布</button>
    </uni-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        
        content: '',
        is_anonymous: false
      }
    }
  },
  methods: {
    onAnonymousChange(e) {
      this.form.is_anonymous = e.detail.value
    },
    // 处理文本输入
    onContentInput(e) {
      this.form.content = e.detail.value
    },
    async handlePublish() {
      const db = uniCloud.database()
      const currentUser = uniCloud.getCurrentUserInfo()
      
      // 处理换行符
      const formattedContent = this.form.content.replace(/\n/g, '\n')
      
      const res = await db.collection('opendb-news-articles').add({
        user_id: this.form.is_anonymous ? 'anonymous_id' : currentUser.uid,
        title: this.form.title,
        content: formattedContent,
        article_status: 1,
        comment_status: this.form.is_anonymous ? 0 : 1,
        publish_date: Date.now(),
        is_anonymous: this.form.is_anonymous
      })
      
      uni.showToast({ title: '发布成功' })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
    }
  }
}
</script>

<style lang="scss">
.publish-container {
  padding: 30rpx;
  
  .title-input {
    margin-bottom: 40rpx;
    ::v-deep .uni-easyinput__content {
      border-bottom: 2rpx solid #eee;
      padding-bottom: 20rpx;
    }
  }
  
  .content-editor {
    margin: 40rpx 0;
    background: #fff;
    border-radius: 16rpx;
    overflow: hidden;
    
    .text-area {
      width: 100%;
      min-height: 400rpx;
      font-size: 28rpx;
      line-height: 1.8;
      padding: 30rpx;
      box-sizing: border-box;
      border: none;
    }
  }
  
  button[type="primary"] {
    margin-top: 60rpx;
    background: #6c8eef;
    border-radius: 50rpx;
  }
}

/* 详情页展示样式 */
.article-container {
  .article-title {
    font-size: 40rpx;
    line-height: 1.4;
    margin-bottom: 40rpx;
    padding-bottom: 20rpx;
    border-bottom: 2rpx solid #eee;
  }
  
  .article-content {
    white-space: pre-line;
    line-height: 1.8;
    font-size: 30rpx;
    color: #333;
  }
}
</style>